<?php
namespace app\admin\controller;
use app\Base;
use think\facade\Db;
class News extends Base{
	private $catlist=array();
    public function index()
    {
		if(!$this->request->post('pageSize') || !$this->request->post('page'))
		{
			return $this->kerror('参数错误');
		}
		
		$pageSet=array('page'=>$this->request->post('page'),'list_rows'=>$this->request->post('pageSize'));
		
		$where=[];
		if($this->request->post('catid'))
		{
			$where[]=array('catid','=',$this->request->post('catid'));
		}
		
		if($this->request->post('title'))
		{
			$where[]=array('title','like',"%".$this->request->post('title')."%");
		}
		
		$order=$this->request->post('order')?$this->request->post('order'):'listorder';
		
		$dataList=Db::name('news')->where($where)->order($order,"desc")->paginate($pageSet)->each(function($item,$key){			
			$cat=Db::name('cat')->where("catid",$item['catid'])->find();
			$item['catname']=$cat['catname']?$cat['catname']:'---';
			$item['rq']=date("Y-m-d",$item['inputtime']);
			return $item;
		})->toArray()['data'];
		
		$total=Db::name('news')->where($where)->count();
		
		$this->setobj(array('dataList'=>$dataList,'total'=>$total));
				
		return $this->ok();
    }
	
	public function catList()
	{
		$this->catlist=[];
		$this->catlist[]=array('catid'=>0,'catname'=>'请选择分类');
		
		$catlist=Db::name("cat")->where("type",1)->field("catid,catname")->order("catid")->select()->each(function($item,$key){
			$total=Db::name("cat")->where("parentid",$item['catid'])->count();
			if($total==0)
			{
				$this->catlist[]=$item;
			}	
			
		});
		
		$this->setobj($this->catlist);
		return $this->ok();
	}
	
	public function newInsert()
	{
		if(!$this->request->post('title') || !$this->request->post('catid'))
		{
			return $this->kerror('参数错误');
		}
		
		$news=array('title'=>$this->request->post('title'),'inputtime'=>time(),'thumb'=>$this->request->post('thumb'),'catid'=>$this->request->post('catid'),'content'=>$this->request->post('content'));
		$id=Db::name('news')->insertGetId($news);
		
		$files=$this->request->post('imgs');		
		if($files)
		{
			$vkey=md5("newsid=".$id);
			foreach($files as $key=>$val)
			{
				$arr=array('name'=>$vkey,'file'=>$val);
				Db::name('file')->insert($arr);
			}
		}
		
		return $this->ok();
		
	}
	
	public function delteNews()
	{
		$ids=$this->request->post('ids');
		
		if(!$ids)
		{
			return $this->kerror('参数错误');
		}
		
		 
		$site=Db::name('site')->where("siteid",1)->find();
		$site=$site['domain'].$site['path'];
		
		if(is_array($ids))
		{
			foreach($ids as $key=>$val)
			{
			Db::name("news")->where("id",$val)->delete();
			$vkey=md5("newsid=".$val);
			$filelist=Db::name('file')->where("name",$vkey)->select();
			
			foreach($filelist as $k=>$v)
			{
				$file=str_replace($site,"./",$v['file']);
				@unlink($file);
			}
			Db::name('file')->where("name",$vkey)->delete();
			}
		}
		else{
			return $this->kerror('参数错误');
		}
		
		
		return $this->ok();
	}
	
	public function editNews()
	{
		$id=$this->request->post('id');
		if(!$id)
		{
			return $this->kerror('参数错误');
		}
		
		if($this->request->post('title'))
		{
			$news=array('title'=>$this->request->post('title'),'listorder'=>$this->request->post('listorder'),'thumb'=>$this->request->post('thumb'),'catid'=>$this->request->post('catid'),'content'=>$this->request->post('content'));
			Db::name("news")->where("id",$id)->update($news);
			$files=$this->request->post('imgs');		
			if($files)
			{
				$vkey=md5("newsid=".$id);
				foreach($files as $key=>$val)
				{
					$arr=array('name'=>$vkey,'file'=>$val);
					Db::name('file')->insert($arr);
				}
			}
		}
		else{
			$info=Db::name("news")->where("id",$id)->find();
			if(!$info['id'])
			{
				return $this->kerror('文章不存在');
			}
			$this->setobj($info);
		}
		return $this->ok();
	}
	
}
